import axios from 'axios'
import type { SimpleResult } from '~/models/Task/TaskManager'

// export const baseUrl = 'http://host.python-helper.internal:31828'
export const baseUrl = 'http://127.0.0.1:31828/'
// const baseUrl = 'http://localhost:5000'

axios.interceptors.request.use((config) => {
  config.baseURL = baseUrl
  return config
}, (error) => {
  return Promise.reject(error)
})

export async function safe(action: () => Promise<void>): Promise<boolean> {
  try {
    await action()
    return true
  }
  catch (err) {
    console.error('falseOnCatch')
    console.error(err)
    return false
  }
}



// v2
export async function apiServerExec(code: string): Promise<SimpleResult> {
  const responce = await axios.post(`/server_exec`, {
    code,
  })
  return responce.data
}

// v2
export async function apiServerStartProcess(code: string): Promise<SimpleResult> {
  const responce = await axios.post(`/server_start_process`, {
    code,
  })
  return responce.data
}

// v2
export async function apiServerStopProcess(id: string): Promise<SimpleResult> {
  const responce = await axios.post(`/server_stop_process`, {
    cell: id
  })
  return responce.data
}

export async function loadCode(path: string): Promise<string> {
  const responce = await fetch(`/python/${path}.py`)
  const text = await responce.text()
  return text
}